package com.chuanyang.driverh.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.view.WindowManager;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 图片工具类
 * @author Kyle
 * @date 2015-7-17 下午8:26:48
 */
public class ImageUtils {
    /**
     * 根据路径获得突破并压缩返回bitmap用于显示
     * @param filePath
     * @param width
     * @param height
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath, int width, int height) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, width, height);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }
    /**
     * 计算图片的缩放值
     *
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

    public static Bitmap getSmallBitmap(String filePath) {
        return getSmallBitmap(filePath, 480, 800);
    }

    public static Bitmap getSmallBitmap(Context context, Uri uri, int width,
                                        int height) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        ParcelFileDescriptor parcelFileDescriptor = null;
        Bitmap image = null;
        try {
            parcelFileDescriptor = context.getContentResolver()
                    .openFileDescriptor(uri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor
                    .getFileDescriptor();
            BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, width, height);
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            image = BitmapFactory.decodeFileDescriptor(fileDescriptor, null,
                    options);
            parcelFileDescriptor.close();
        } catch (FileNotFoundException e) {
           PrintUtil.toast(context, "没有找到文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return image;
    }

    public static Bitmap getSmallBitmap(Context context, Uri uri) {
        return getSmallBitmap(context, uri, 480, 800);
    }
    /**获取一个刚好填充页面的背景

     */
    public static Bitmap getBitmapFullScreen(Context context, int resId)
    {

        //(1)动态获取手机的宽和高  windowmanager  smsmanger telephonemanager
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        //创建一个位图工厂的配置参数
        BitmapFactory.Options opts = new BitmapFactory.Options();
        //设置true 不去真正的解析位图 返回no bitmap   但是还能够获取一些图片的宽高信息
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(context.getResources(),resId,opts);
        //(2)获取图片的宽和高
        int imgWidth = opts.outWidth;
        int imgHeight = opts.outHeight;

        //(3)计算缩放比
        int scale = 1;
        int scalex = imgWidth/width;
        int scaley = imgHeight/height;
        //(4)对缩放比进行判断
        if (scalex>=scaley && scalex>1) {
            scale = scalex;
        }

        if (scaley>scalex && scaley>1) {
            scale =scaley;

        }

        System.out.println("缩放比为:"+scale);

        //(5)按照我们计算出来的缩放比进行显示
        opts.inSampleSize  = scale;

        //(6)开始真正解析位图
        opts.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId,opts);
        return  bitmap;
    }
    public static Bitmap getDesignedSize(Context context, int width, int height, int resid)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(context.getResources(), resid,options);
        options.inSampleSize = calculateInSampleSize(options, width, height);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resid,options);
        return  bitmap;
    }
}
